pub async fn new(config: ConsulConfig) -> Result<Self, ClusterError> {
    // Create client settings - 将临时值存储为变量
    let mut settings_builder = ConsulClientSettingsBuilder::default();
    settings_builder = settings_builder.address(&config.address);
    
    // Add token if provided
    if let Some(token) = &config.token {
        settings_builder = settings_builder.token(token);
    }
    
    // Add datacenter if provided
    if let Some(dc) = &config.datacenter {
        settings_builder = settings_builder.datacenter(dc);
    }
    
    // Build settings and client
    let settings = settings_builder.build()?;
    let client = ConsulClient::new(settings)?;
    
    // Create channels for membership events
    let (event_tx, event_rx) = mpsc::channel(100);
    
    Ok(Self {
        client,
        consul_config: config,
        self_id: RwLock::new(None),
        event_tx,
        event_rx: RwLock::new(Some(event_rx)),
        members_cache: Arc::new(RwLock::new(HashMap::new())),
    })
}

/// Convert Consul service to Member
fn service_to_member(&self, service: consulrs::api::service::common::AgentService) -> Member {
    let mut member = Member::new(
        service.id.clone().unwrap_or_else(|| "unknown".to_string()),
        service.address.clone().unwrap_or_else(|| service.id.clone().unwrap_or_else(|| "unknown".to_string())),
        service.port.unwrap_or(0) as u16,  // 转换为u16
    );
    
    // ... 其他处理逻辑保持不变 ...
    member
}

pub fn start_discovery(
    self: Arc<Self>,
    config: ClusterConfig
) -> tokio::task::JoinHandle<()> {
    // 不再尝试 clone client
    let members_cache = self.members_cache.clone();
    let event_tx = self.event_tx.clone();
    let service_name = self.get_service_name(&config.name);
    let service_prefix = service_name.clone();
    
    // 强制所有权移动，拿到 Arc<Self> 的引用
    let provider = self.clone();
    
    // Spawn discovery task
    tokio::spawn(async move {
        // ... existing code ...
        
        // 在循环中访问 provider.client 而不是克隆它
        loop {
            // ... existing code ...
            
            // 修改这部分代码以处理服务列表
            match catalog::services(&provider.client).await {
                Ok(services) => {
                    // ... existing code ...
                    let mut current_members = HashMap::new();
                    
                    for (name, tags) in services {
                        if name.starts_with(&service_prefix) {
                            // 获取服务详情
                            if let Ok(service_list) = catalog::service(&provider.client, &name).await {
                                for service in service_list {
                                    let member = provider.service_to_member(service);
                                    current_members.insert(member.id.clone(), member);
                                }
                            }
                        }
                    }
                    
                    // ... existing code ...
                }
                // ... existing code ...
            }
            
            // ... existing code ...
        }
    })
}

async fn deregister(&self) -> Result<(), ClusterError> {
    // ... existing code ...
    
    // Deregister from Consul - 添加必要的第三个参数
    service::deregister(&self.client, &service_id, None)
        .await
        .map_err(|e| ClusterError::ProviderError(format!("Failed to deregister from Consul: {}", e)))?;
    
    // ... existing code ...
    Ok(())
} 